/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       multiloader.cpp
 * Author:     karooolek
 * Created on: 2009-03-22
 *
 **********************************************************************************************************************/

#include "multiloader.h"

namespace mGameEngine
{

MultiLoader::MultiLoader() : _loaders(0), _activeLoader(NULL)
{
}

MultiLoader::~MultiLoader()
{
}

void MultiLoader::addLoader(Loader *loader)
{
    // no null loaders
    if(!loader)
    {
        return;
    }

    _loaders.push_back(loader);
}

void MultiLoader::removeLoader(Loader *loader)
{
    // reset active loader
    if(_activeLoader == loader)
    {
        _activeLoader = NULL;
    }

    _loaders.remove(loader);
}

void MultiLoader::load()
{
    // no sub-loaders
    if(_loaders.empty())
    {
        return;
    }

    // load all sub-loaders
    uint n = _loaders.size();
    uint k = 0;
    for(List<Loader *>::ConstIterator i = _loaders.begin(); i != _loaders.end(); ++i)
    {
        Loader *loader = *i;
        _activeLoader = loader;
        loader->load();
        _state = (float)(++k)/(float)n;
    }

    // reset active sub-loader
    _activeLoader = NULL;
}

void MultiLoader::unload()
{
    // no sub-loaders
    if(_loaders.empty())
    {
        return;
    }

    // load all sub-loaders
    uint n = _loaders.size();
    uint k = n;
    for(List<Loader *>::ConstIterator i = _loaders.begin(); i != _loaders.end(); ++i)
    {
        Loader *loader = *i;
        _activeLoader = loader;
        loader->unload();
        _state = (float)(--k)/(float)n;
    }

    // reset active sub-loader
    _activeLoader = NULL;
}

}


